package com.zym.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class TestAspect {

    @Pointcut("execution(* com.zym.controller.TestController.test1(..))")
    public void pointcutMethod(){
    }
    @Before(value="pointcutMethod()")
    public void beforeMethod(JoinPoint joinPoint){
        String s = joinPoint.toString();
        Object[] args = joinPoint.getArgs();
        String kind = joinPoint.getKind();
        Object aThis = joinPoint.getThis();
        System.out.println("前置通知");
    }
    @After(value="pointcutMethod()")
    public void afterMethod(JoinPoint joinPoint){
        System.out.println("后置通知");
    }
    @AfterReturning(value="pointcutMethod()",returning = "re")
    public void afterReturningMethod(JoinPoint joinPoint, Object re){
        System.out.println("方法执行完成，在返回值之前的通知"+re);
    }
    @AfterThrowing(value="pointcutMethod()",throwing = "ex")
    public void afterThrowingMethod(JoinPoint joinPoint,Exception ex){
        System.out.println("方法发生异常的通知"+ex.getMessage());
    }
    @Around(value="pointcutMethod()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint){
        Object proceed =null;
       try{
           System.out.println("环绕的前置通知");
             proceed = joinPoint.proceed();
           System.out.println("环绕的返回之后的通知");

       } catch (Throwable throwable) {
           throwable.printStackTrace();
           System.out.println("环绕的异常通知");
       } finally {
           System.out.println("环绕的后置通知");
       }
       return proceed;
    }
}
